﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using MyMusicCollection.Model;
using System.IO;

namespace MyMusicCollection
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private SearchHandler mSearchHandler = new SearchHandler();
		private ArtistHandler mArtistHandler = new ArtistHandler();
		private AlbumHandler mAlbumHandler = new AlbumHandler();
		private TrackHandler mTrackHandler = new TrackHandler();
		private CollectionHandler mCollectionHandler = new CollectionHandler();
		private SearchResult mSelectedSearchResult;
		private ArtistAlbum mSelectedAlbum;
		private AlbumTrack mSelectedAlbumTrack;

		private static int LAST_COLLECTION = 0;
		private static int LAST_ARTIST = 1;

		private int mLastScreen;

		public MainWindow()
		{
			InitializeComponent();
			WindowStartupLocation = WindowStartupLocation.CenterScreen;
			
			SetStatusLabel(); //Checkar uppkoppling

			ActivateCollection();
		}

		//********************************
		//************* VYER *************
		//********************************
		#region Vyer
		private void ActivateCollection()
		{
			mLastScreen = LAST_COLLECTION;

			HideAllTabs();
			mCollectionTab.Visibility = Visibility.Visible;
			mCollectionTab.IsSelected = true;

			mCollectionAlbumListBox.Items.Clear();

			List<ArtistAlbum> collectionAlbumList = new List<ArtistAlbum>();
			collectionAlbumList = mCollectionHandler.GetCollection();

			if (collectionAlbumList != null)
			{
				foreach (ArtistAlbum album in collectionAlbumList)
				{
					ListBoxItem item = new ListBoxItem();
					StackPanel sp = new StackPanel();
					DockPanel dp1 = new DockPanel();
					DockPanel dp2 = new DockPanel();
					TextBlock tb1 = new TextBlock();
					TextBlock tb2 = new TextBlock();
					TextBlock tb3 = new TextBlock();

					tb1.Text = album.mArtist + " - " + album.mTitle + " - ";
					tb1.FontWeight = FontWeights.Bold;
					tb2.Text = album.mYear;
					tb2.FontStyle = FontStyles.Italic;
					tb3.Text = album.mFormat + " - " + album.mLabel;

					dp1.Children.Add(tb1);
					dp1.Children.Add(tb2);
					dp2.Children.Add(tb3);

					sp.Children.Add(dp1);
					sp.Children.Add(dp2);

					item.Margin = new System.Windows.Thickness { Bottom = 10 };
					item.Content = sp;
					item.Tag = album;

					mCollectionAlbumListBox.Items.Add(item);
				}
			}
			else
			{
				//Finns ingen samling
				ListBoxItem item = new ListBoxItem();
				item.Content = "Du har inte lagt till något till din samling än.";
				mCollectionAlbumListBox.Items.Add(item);
			}
		}

		private void ActivateSearch()
		{
			HideAllTabs();
			mSearchTab.Visibility = Visibility.Visible;
			mSearchTab.IsSelected = true;

			mSearchListBox.Items.Clear();

			List<SearchResult> searchResult = new List<SearchResult>();

			//Söker efter rätt sak
			if (mArtistRadioButton.IsChecked == true)
			{
				searchResult = mSearchHandler.DoSearch(mSearchTextBox.Text, "artists");
			}
			else if (mAlbumRadioButton.IsChecked == true)
			{
				searchResult = mSearchHandler.DoSearch(mSearchTextBox.Text, "releases");
			}
			else
			{
				searchResult = mSearchHandler.DoSearch(mSearchTextBox.Text, "all");
			}

			//Om sökningen inte ger några resultat presenteras detta för användaren
			if (searchResult == null || searchResult.Count <= 0)
			{
				ListBoxItem item = new ListBoxItem();
				item.Content = "Inga resultat!";
				mSearchListBox.Items.Add(item);
			}
			else
			{
				foreach (SearchResult sr in searchResult)
				{
					ListBoxItem item = new ListBoxItem();
					item.Content = sr.mName;
					item.Tag = sr;
					mSearchListBox.Items.Add(item);
				}
			}
		}

		private void ActivateWishlist()
		{
			HideAllTabs();
			mWishlistTab.Visibility = Visibility.Visible;
			mWishlistTab.IsSelected = true;
		}

		private void ActivateArtist(Artist aArtist)
		{
			mLastScreen = LAST_ARTIST;

			if (aArtist != null)
			{
				mArtistTab.Visibility = Visibility.Visible;
				mArtistTab.IsSelected = true;
				if (aArtist.mName == null)
				{
					mArtistTab.Header = "Artist";
					mArtistNameLabel.Content = "artistnamn_ej_tillgängligt";
				}
				else
				{
					mArtistTab.Header = aArtist.mName;
					mArtistNameLabel.Content = aArtist.mName;
				}

				mArtistAlbumListBox.Items.Clear();

				//Visar albuminfo
				foreach (ArtistAlbum album in aArtist.mAlbumList)
				{
					ListBoxItem item = new ListBoxItem();
					StackPanel sp = new StackPanel();
					DockPanel dp1 = new DockPanel();
					DockPanel dp2 = new DockPanel();
					TextBlock tb1 = new TextBlock();
					TextBlock tb2 = new TextBlock();
					TextBlock tb3 = new TextBlock();

					tb1.Text = album.mTitle + " - ";
					tb1.FontWeight = FontWeights.Bold;
					tb2.Text = album.mYear;
					tb2.FontStyle = FontStyles.Italic;
					tb3.Text = album.mFormat + " - " + album.mLabel;

					dp1.Children.Add(tb1);
					dp1.Children.Add(tb2);
					dp2.Children.Add(tb3);

					sp.Children.Add(dp1);
					sp.Children.Add(dp2);

					item.Margin = new System.Windows.Thickness { Bottom = 10 };
					item.Content = sp;
					item.Tag = album;

					//Checkar vilken typ det är
					if (album.mFormat.Contains("Album") == true)
					{
						mArtistAlbumListBox.Items.Add(item);
					}
					else if (album.mFormat.Contains("Single") == true)
					{
						mArtistSingleListBox.Items.Add(item);
					}
					else
					{
						mArtistMiscListBox.Items.Add(item);
					}
				}
			}

			//mArtistAlbumListBox.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Content.Children.Children.Text", System.ComponentModel.ListSortDirection.Ascending));
		}

		private void ActivateAlbum(Album aAlbum)
		{
			mAlbumTab.Visibility = Visibility.Visible;
			mAlbumTab.IsSelected = true;
			if (aAlbum.mTitle == "titel_ej_tillgänglig")
			{
				mAlbumTab.Header = "Album";
			}
			else
			{
				mAlbumTab.Header = aAlbum.mTitle;
			}

			mAlbumTrackListBox.Items.Clear();

			//Döljer lägg tillknappen om albumet redan är tillagt
			if (aAlbum.mInCollection == true)
			{
				mAlredayInCollectionLabel.Visibility = Visibility.Visible;
				mAddToCollectionButton.Visibility = Visibility.Hidden;
			}
			else
			{
				mAlredayInCollectionLabel.Visibility = Visibility.Hidden;
				mAddToCollectionButton.Visibility = Visibility.Visible;
			}

			//Omslag (tom bild satt i xaml)
			if (aAlbum.mImageUri != null)
			{
				mAlbumImage.Source = new ImageSourceConverter().ConvertFromString(aAlbum.mImageUri) as ImageSource;
			}

			//Övriga egenskaper
			mAlbumTitle.Content = aAlbum.mTitle;
			mAlbumArtistName.Content = aAlbum.mArtist;
			mAlbumYear.Content = aAlbum.mYear;
			mAlbumGenre.Content = aAlbum.mGenre;
			mAlbumLabel.Content = aAlbum.mLabel;
			mAlbumFormat.Content = aAlbum.mFormat;

			//Låtar
			foreach (AlbumTrack track in aAlbum.mTrackList)
			{
				ListBoxItem item = new ListBoxItem();
				StackPanel sp = new StackPanel();
				DockPanel dp1 = new DockPanel();
				DockPanel dp2 = new DockPanel();
				TextBlock tb1 = new TextBlock();
				TextBlock tb2 = new TextBlock();
				TextBlock tb3 = new TextBlock();

				tb1.Text = track.mPosition + ". ";

				tb2.Text = track.mTitle + " - ";
				tb2.FontWeight = FontWeights.Bold;

				tb3.Text = track.mDuration;
				tb3.FontStyle = FontStyles.Italic;

				dp1.Children.Add(tb1);
				dp1.Children.Add(tb2);
				dp1.Children.Add(tb3);

				sp.Children.Add(dp1);

				item.Margin = new System.Windows.Thickness { Bottom = 10 };
				item.Content = sp;
				item.Tag = track;

				mAlbumTrackListBox.Items.Add(item);
			}
		}

		private void ActivateTrack(Track aTrack)
		{
			mTrackTab.Visibility = Visibility.Visible;
			mTrackTab.IsSelected = true;
			mTrackTab.Header = aTrack.mTitle;

			mTrackTitleLabel.Content = aTrack.mTitle;
			mTrackAlbumLabel.Content = aTrack.mAlbum;
			mTrackArtistLabel.Content = aTrack.mArtist;
			mTrackLyricsTextBlock.Text = aTrack.mLyrics;
		}
		#endregion



		//********************************
		//********* CLICKEVENTS **********
		//********************************
		#region ClickEvents
		private void mSearchButton_Click(object sender, RoutedEventArgs e)
		{
			ActivateSearch();
		}

		private void mCollectionButton_Click(object sender, RoutedEventArgs e)
		{
			ActivateCollection();
		}

		private void mWishlistButton_Click(object sender, RoutedEventArgs e)
		{
			ActivateWishlist();
		}

		private void mSearchListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			foreach (ListBoxItem item in mSearchListBox.SelectedItems)
			{
				mSelectedSearchResult = (SearchResult)item.Tag;
				ActivateArtist(mArtistHandler.GetArtist(mSelectedSearchResult.mUri));
			}

			mSearchListBox.SelectedItem = null;
		}

		private void mArtistAlbumListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			foreach (ListBoxItem item in mArtistAlbumListBox.SelectedItems)
			{
				mSelectedAlbum = (ArtistAlbum)item.Tag;
				ActivateAlbum(mAlbumHandler.GetAlbum(mSelectedAlbum.mID.ToString()));
			}

			mArtistAlbumListBox.SelectedItem = null;
		}

		private void mArtistSingleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			foreach (ListBoxItem item in mArtistSingleListBox.SelectedItems)
			{
				mSelectedAlbum = (ArtistAlbum)item.Tag;
				ActivateAlbum(mAlbumHandler.GetAlbum(mSelectedAlbum.mID.ToString()));
			}

			mArtistSingleListBox.SelectedItem = null;
		}

		private void mArtistMiscListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			foreach (ListBoxItem item in mArtistMiscListBox.SelectedItems)
			{
				mSelectedAlbum = (ArtistAlbum)item.Tag;
				ActivateAlbum(mAlbumHandler.GetAlbum(mSelectedAlbum.mID.ToString()));
			}

			mArtistMiscListBox.SelectedItem = null;
		}

		private void mAlbumTrackListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			foreach (ListBoxItem item in mAlbumTrackListBox.SelectedItems)
			{
				mSelectedAlbumTrack = (AlbumTrack)item.Tag;

				Track at = mTrackHandler.GetTrack(mSelectedAlbumTrack);

				if (at != null)
				{
					ActivateTrack(at);
				}
			}

			mAlbumTrackListBox.SelectedItem = null;
		}

		private void mCollectionAlbumListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			foreach (ListBoxItem item in mCollectionAlbumListBox.SelectedItems)
			{
				mSelectedAlbum = (ArtistAlbum)item.Tag;
				ActivateAlbum(mAlbumHandler.GetAlbum(mSelectedAlbum.mID.ToString()));
			}

			mArtistMiscListBox.SelectedItem = null;
		}

		private void mCloseTabButtonArtist_Click(object sender, RoutedEventArgs e)
		{
			mArtistTab.Visibility = Visibility.Collapsed;
			mSearchTab.IsSelected = true;
		}

		private void mCloseTabButtonAlbum_Click(object sender, RoutedEventArgs e)
		{
			//Går tillbaka till rätt skärm
			if (mLastScreen == LAST_ARTIST)
			{
				mAlbumTab.Visibility = Visibility.Collapsed;
				mArtistTab.IsSelected = true;
			}
			else
			{
				mAlbumTab.Visibility = Visibility.Collapsed;
				mCollectionTab.IsSelected = true;
			}
		}

		private void mCloseTabButtonTrack_Click(object sender, RoutedEventArgs e)
		{
			mTrackTab.Visibility = Visibility.Collapsed;
			mAlbumTab.IsSelected = true;
		}

		private void mAddToCollectionButton_Click(object sender, RoutedEventArgs e)
		{
			mCollectionHandler.AddToCollection(mSelectedAlbum);
			ActivateAlbum(mAlbumHandler.GetAlbum(mSelectedAlbum.mID.ToString()));
		}
		#endregion



		//********************************
		//************ ÖVRIGT ************
		//********************************
		#region Övrigt
		//Sätter statuslabeln nere i högerhörnet
		private void SetStatusLabel()
		{
			if (Model.InternetStatusChecker.CheckStatus() == true)
			{
				mStatusLable.Content = "Status: Online";
				mStatusLable.Foreground = Brushes.Green;
			}
			else
			{
				mStatusLable.Content = "Status: Offline";
				mStatusLable.Foreground = Brushes.Red;
			}
		}

		//Gömmer alla tabbar
		private void HideAllTabs()
		{
			mCollectionTab.Visibility = Visibility.Collapsed;
			mWishlistTab.Visibility = Visibility.Collapsed;
			mSearchTab.Visibility = Visibility.Collapsed;
			mArtistTab.Visibility = Visibility.Collapsed;
			mAlbumTab.Visibility = Visibility.Collapsed;
			mTrackTab.Visibility = Visibility.Collapsed;
		}
		#endregion

		
	}
}
